package com.shiku.imserver.common.packets;

import com.shiku.imserver.common.Protocol;
import com.shiku.imserver.common.message.ChatMessage;
import org.tio.core.intf.Packet;

public class ImPacket extends Packet implements IPacket {
    private static final long serialVersionUID = 2000118564569232098L;
    protected byte[] bytes;
    private short command;
    private ChatMessage message;

    public ImPacket() {
    }

    public ImPacket(byte[] bytes2) {
        this.bytes = bytes2;
    }

    public ImPacket(short command2, byte[] body) {
        this(body);
        setCommand(command2);
    }

    public ImPacket(short command2) {
        this(command2, null);
    }

    public void releaseMessageBody() {
        setBytes(null);
    }

    public static byte encodeEncrypt(byte bs, boolean isEncrypt) {
        if (isEncrypt) {
            return (byte) (bs | Byte.MIN_VALUE);
        }
        return 0;
    }

    public static boolean decodeCompress(byte version) {
        return (version & Protocol.FIRST_BYTE_MASK_COMPRESS) != 0;
    }

    public static byte encodeCompress(byte bs, boolean isCompress) {
        if (isCompress) {
            return (byte) (bs | Protocol.FIRST_BYTE_MASK_COMPRESS);
        }
        return (byte) (bs & 63);
    }

    public static boolean decodeHasSynSeq(byte maskByte) {
        return (maskByte & Protocol.FIRST_BYTE_MASK_HAS_SYNSEQ) != 0;
    }

    public static byte encodeHasSynSeq(byte bs, boolean hasSynSeq) {
        if (hasSynSeq) {
            return (byte) (bs | Protocol.FIRST_BYTE_MASK_HAS_SYNSEQ);
        }
        return (byte) (bs & 95);
    }

    public static boolean decode4ByteLength(byte version) {
        return (version & Protocol.FIRST_BYTE_MASK_4_BYTE_LENGTH) != 0;
    }

    public static byte encode4ByteLength(byte bs, boolean is4ByteLength) {
        if (is4ByteLength) {
            return (byte) (bs | Protocol.FIRST_BYTE_MASK_4_BYTE_LENGTH);
        }
        return (byte) (bs & 111);
    }

    public static byte decodeVersion(byte version) {
        return (byte) (version & Protocol.FIRST_BYTE_MASK_VERSION);
    }

    public int calcHeaderLength(boolean is4byteLength) {
        int ret = 4;
        if (is4byteLength) {
            ret = 4 + 2;
        }
        if (getSynSeq().intValue() > 0) {
            return ret + 4;
        }
        return ret;
    }

    public short getCommand() {
        return this.command;
    }

    public void setCommand(short type) {
        this.command = type;
    }

    public byte[] getBytes() {
        return this.bytes;
    }

    public void setBytes(byte[] bytes2) {
        this.bytes = bytes2;
    }

    public String logstr() {
        return String.valueOf((int) this.command);
    }

    public ChatMessage getMessage() {
        return this.message;
    }

    public void setMessage(ChatMessage message2) {
        this.message = message2;
    }
}
